package start;

import qunar.tc.qmq.configuration.DynamicConfig;
import qunar.tc.qmq.configuration.DynamicConfigLoader;
import qunar.tc.qmq.meta.startup.Bootstrap;
import qunar.tc.qmq.meta.startup.ServerWrapper;
import qunar.tc.qmq.meta.web.MetaManagementServlet;
import qunar.tc.qmq.meta.web.MetaServerAddressSupplierServlet;
import qunar.tc.qmq.meta.web.OnOfflineServlet;
import qunar.tc.qmq.meta.web.SubjectConsumerServlet;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class MetaStart {
    public static void main(String[] args) throws Exception {
        final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        context.setResourceBase(System.getProperty("java.io.tmpdir"));
        DynamicConfig config = DynamicConfigLoader.load("metaserver.properties");
        final ServerWrapper wrapper = new ServerWrapper(config);
        wrapper.start(context.getServletContext());

        context.addServlet(MetaServerAddressSupplierServlet.class, "/meta/address");
        context.addServlet(MetaManagementServlet.class, "/management");
        context.addServlet(SubjectConsumerServlet.class, "/subject/consumers");
        context.addServlet(OnOfflineServlet.class, "/onoffline");

        // TODO(keli.wang): allow set port use env
        int port = config.getInt("meta.server.discover.port", 8080);
        final Server server = new Server(port);
        server.setHandler(context);
        server.start();
        server.join();
    }
}
